package peer.gui;

import java.util.ArrayList;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import peer.SharingManager.FileReference;

import common.Util;
import peer.SharingManager.*;

public class LibraryModel implements TableModel
{
	ArrayList<FileReference> data = new ArrayList<FileReference>();
	String[] columnNames = {"File name", "Size", "Hash code"};
	Class[] columnClasses = {String.class, Integer.class, StringBuilder.class};
	
	TableModelListener listener;

	public void addTableModelListener(TableModelListener listener)
	{
		this.listener = listener;
	}

	public Class<?> getColumnClass(int i)
	{
		return columnClasses[i];
	}

	public int getColumnCount()
	{
		return columnNames.length;
	}

	public String getColumnName(int i)
	{
		return columnNames[i];
	}

	public int getRowCount()
	{
		return data.size();
	}

	public Object getValueAt(int row, int column)
	{
		switch (column)
		{
		case 0:
			return data.get(row).getFileName();
		case 1:
			return Util.getNiceFileSize(data.get(row).getFileSize());
		case 2:
			return data.get(row).getHashCode();
		}
		return null;
	}

	public boolean isCellEditable(int row, int column)
	{
		return false;
	}
	
	public void removeTableModelListener(TableModelListener l)
	{
		// TODO Auto-generated method stub
		
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex)
	{
		// TODO Auto-generated method stub
		
	}
	
	public void addFile(FileReference fr) {
		// TODO Auto-generated method stub
		data.add(fr);
		listener.tableChanged(new TableModelEvent(this, data.size(), data.size(), TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
	}

	

}
